//package com.bristua.barcode.common.util;
//
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.ydsh.wal.common.constants.Constants;
//import org.apache.commons.httpclient.HttpClient;
//import org.apache.commons.httpclient.HttpException;
//import org.apache.commons.httpclient.HttpStatus;
//import org.apache.commons.httpclient.methods.PostMethod;
//import org.apache.commons.httpclient.methods.RequestEntity;
//import org.apache.commons.httpclient.methods.StringRequestEntity;
//import org.apache.commons.httpclient.params.HttpClientParams;
//import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import javax.net.ssl.HttpsURLConnection;
//import java.io.BufferedInputStream;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.io.UnsupportedEncodingException;
//import java.net.SocketTimeoutException;
//import java.net.URL;
//import java.util.HashMap;
//import java.util.Iterator;
//import java.util.Map;
//import java.util.Map.Entry;
//import java.util.Set;
//
///**
// * @author hrt
// * @date 2018年1月5日
// * @description
// * http请求工具类
// */
//public class HttpUtils {
//
//	private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
//	/**
//	 * 超时时间
//	 */
//	private static final int timeOut = 1000*20;
//	/**
//	 * 超时编码
//	 */
//	public static final String CODE_TIMEOUT = "TIMEOUT8888";
//
//	/**
//	 * POST请求GBK
//     * 发送http报文
//     * @param reqData 请求数据
//     * @param url 请求地址
//     * @return
//     * @throws IOException
//     */
//    public static String excute(String reqData, String url) throws IOException {
//
//		HttpClientParams httpClientParams = new HttpClientParams();
//		String encoding = Constants.CHARSET_GBK;
//		String contentType = Constants.CONTENTTYPE_TEXT;
//		httpClientParams.setContentCharset(encoding);
//		httpClientParams.setHttpElementCharset(encoding);
//		HttpClient httpClient = new HttpClient(httpClientParams);
//		httpClient.getHostConfiguration().setHost(url);
//		HttpConnectionManagerParams httpConnectionManagerParams = new HttpConnectionManagerParams();
//		httpConnectionManagerParams.setConnectionTimeout(timeOut);
//		httpConnectionManagerParams.setDefaultMaxConnectionsPerHost(1);
//		httpConnectionManagerParams.setMaxTotalConnections(20);
//		httpConnectionManagerParams.setSoTimeout(timeOut);
//		httpClient.getHttpConnectionManager().setParams(httpConnectionManagerParams);
//		PostMethod postMethod = null;
//		try {
//			postMethod = new PostMethod(url);
//			RequestEntity requestEntity = new StringRequestEntity(reqData, contentType, encoding);
//			postMethod.setRequestEntity(requestEntity);
//			int httpReturnCode = httpClient.executeMethod(postMethod);
//			if(HttpStatus.SC_OK != httpReturnCode){
//				logger.error("http请求返回错误状态码：{}", httpReturnCode);
//			}
//			String responseBody = postMethod.getResponseBodyAsString();
//			postMethod.releaseConnection();
//			return responseBody;
//		} catch(SocketTimeoutException e) {
//			logger.error("请求超时");
//			return CODE_TIMEOUT;
//		}catch (UnsupportedEncodingException e) {
//			logger.error("编码异常");
//			throw e;
//		} catch (HttpException e) {
//			logger.error("http异常");
//			throw e;
//		} catch (IOException e) {
//			logger.error("io异常");
//			throw e;
//		} finally {
//			postMethod.releaseConnection();
//		}
//	}
//
//    /**
//     * POST请求UT-8
//     * 发送http报文
//     * @param param 请求数据
//     * @param url 请求地址
//     * @return  请求结果字符串
//     * @throws IOException
//     */
//    public static String excute(String url , Map<String, String> param){
//        if (!url.startsWith("http")) {
//            url += "http://";
//        }
//        HttpClientParams httpClientParams = new HttpClientParams();
//        String encoding = Constants.CHARSET_UTF8;
//        httpClientParams.setContentCharset(encoding);
//        httpClientParams.setHttpElementCharset(encoding);
//        HttpClient httpClient = new HttpClient(httpClientParams);
//        httpClient.getHostConfiguration().setHost(url);
//        HttpConnectionManagerParams httpConnectionManagerParams = new HttpConnectionManagerParams();
//        httpConnectionManagerParams.setConnectionTimeout(timeOut);
//        httpConnectionManagerParams.setDefaultMaxConnectionsPerHost(1);
//        httpConnectionManagerParams.setMaxTotalConnections(20);
//        httpConnectionManagerParams.setSoTimeout(timeOut);
//        httpClient.getHttpConnectionManager().setParams(httpConnectionManagerParams);
//        PostMethod postMethod = null;
//        try {
//            postMethod = new PostMethod(url);
//            Set<Entry<String, String>> sets  = param.entrySet();
//            Iterator<Entry<String, String>> it = sets.iterator();
//            while (it.hasNext()) {
//                Entry<String, String> entry = it.next();
//                postMethod.addParameter( entry.getKey() , entry.getValue());
//            }
//            int httpReturnCode = httpClient.executeMethod(postMethod);
//            if(HttpStatus.SC_OK != httpReturnCode){
//                logger.error("http请求返回错误状态码：{}" +  httpReturnCode);
//            }
//            String responseBody = postMethod.getResponseBodyAsString();
//            postMethod.releaseConnection();
//            return responseBody;
//        } catch(Exception e) {
//            logger.error("http请求异常",e);
//            return null;
//        } finally {
//            postMethod.releaseConnection();
//        }
//    }
//
//	/**
//	 * UTF-8
//	 * 发送get请求，返回json
//	 * @param url
//	 * @return
//	 * @throws IOException
//	 */
//	public static Map<String, String> getJson(String url) {
//
//		Map<String, String> result = null;
//		ByteArrayOutputStream bos = null;
//		try{
//			URL reqUrl = new URL(url);
//			HttpsURLConnection httpsConn = (HttpsURLConnection) reqUrl.openConnection();
//			byte[] tmp = new byte[1024];
//			bos = new ByteArrayOutputStream();
//			int len = -1;
//			while((len = httpsConn.getInputStream().read(tmp))!=-1){
//				bos.write(tmp,0,len);
//			}
//			String jsonStr = new String(bos.toByteArray(),Constants.CHARSET_UTF8);
//			ObjectMapper mapper = new ObjectMapper();
//			result = mapper.readValue(jsonStr, new TypeReference<HashMap<String,String>>(){});
//		} catch (Exception e) {
//			logger.error(url + "接口调用失败", e);
//		} finally {
//			try {
//				bos.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		return result;
//	}
//
//	/**
//	 * UTF-8
//	 * 发送post请求，返回json
//	 * @param url
//	 * @param json
//	 * @return
//	 */
//	public static String postJson(String url, String json) {
//		String ret = null;
//		HttpClient client = new HttpClient();
//		PostMethod myPost = new PostMethod(url);
//		try {
//			myPost.setRequestEntity(new StringRequestEntity(json, Constants.CONTENTTYPE_TEXT, Constants.CHARSET_UTF8));
//			int status = client.executeMethod(myPost);
//			if(status == HttpStatus.SC_OK){
//				BufferedInputStream bis = new BufferedInputStream(myPost.getResponseBodyAsStream());
//				byte[] buffer = new byte[1024];
//				ByteArrayOutputStream baos = new ByteArrayOutputStream();
//				int s = 0;
//				while((s=bis.read(buffer))!=-1){
//					baos.write(buffer,0,s);
//				}
//				byte[] data = baos.toByteArray();
//				ret = new String(data, 0, data.length, Constants.CHARSET_UTF8);
//				baos.close();
//				bis.close();
//			}
//		} catch (Exception e) {
//			logger.error(url + "接口调用失败", e);
//		} finally{
//			myPost.releaseConnection();
//			client.getHttpConnectionManager().closeIdleConnections(0);
//		}
//		return ret;
//	}
//
//
//}
